package com.report.reportserver.controller;

import com.report.reportserver.config.security.JwtTokenUtils;
import com.report.reportserver.model.User;
import com.report.reportserver.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @ ClassName: LoginController
 * @ Description：登陆控制类
 * @ Modified By：
 * @ Version:
 * @ Author     ：LiYing
 * @ Date       ：Created in 10:49 2022/3/8
 */
@Controller
public class LoginController {

    @Autowired
    UserService userService;

    @PostMapping("/login")
    @ResponseBody
    public String login(HttpServletRequest request){
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        User user = userService.queryUserByName(username);
        System.out.println(user);
        if(user == null || !user.getUserPassword().equals(password)) {
            return "Error";
        }
        //用户名密码验证通过后，生成token
        String token = JwtTokenUtils.createToken(user);
        return token;
    }

}
